package com.motong.gongdan.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;

/**
 * 来源 https://blog.csdn.net/weixin_44009447/article/details/116708514
 *
 * @Author csdn 博主:Keson Z
 * @Description POI导出excel工具类
 * @Date 17:16 2021/5/11
 * @Param
 * @return
 **/
public class POIUtil {
    /**
     * @return void
     * @Author csdn 博主:Keson Z
     * @Description 拷贝sheet（表）
     * @Date 17:16 2021/5/11
     * @Param [targetSheet, sourceSheet, targetWork, sourceWork, startRow, cellRangeAddressExcelVoList]
     **/
    public static void copySheet(Sheet targetSheet, XSSFSheet sourceSheet,
                                 Workbook targetWork, Workbook sourceWork, int startRow) {
        if (targetSheet == null || sourceSheet == null || targetWork == null || sourceWork == null) {
            throw new IllegalArgumentException("调用PoiUtil.copySheet()方法时，targetSheet、sourceSheet、targetWork、sourceWork都不能为空，故抛出该异常！");
        }

        // 设置单元格默认宽度
        targetSheet.setDefaultColumnWidth(25);
        // 复制源表中的行
        for (int i = sourceSheet.getFirstRowNum(); i <= sourceSheet.getLastRowNum(); i++) {
            Row sourceRow = sourceSheet.getRow(i);
            // 创建新的row
            Row targetRow = targetSheet.createRow(i + startRow);
            if (sourceRow != null) {
                copyRow(targetRow, sourceRow,
                        targetWork, sourceWork);
            }
        }
    }


    /**
     * @return void
     * @Author csdn 博主:Keson Z
     * @Description 拷贝row(行)
     * @Date 17:17 2021/5/11
     * @Param [targetRow, sourceRow, targetWork, sourceWork]
     **/
    public static void copyRow(Row targetRow, Row sourceRow,
                               Workbook targetWork, Workbook sourceWork) {
        if (targetRow == null || sourceRow == null || targetWork == null || sourceWork == null) {
            throw new IllegalArgumentException("调用PoiUtil.copyRow()方法时，targetRow、sourceRow、targetWork、sourceWork、targetPatriarch都不能为空，故抛出该异常！");
        }

        //设置行高
        targetRow.setHeight(sourceRow.getHeight());

        for (int i = sourceRow.getFirstCellNum(); i < sourceRow.getLastCellNum(); i++) {
            Cell sourceCell = sourceRow.getCell(i);
            Cell targetCell = null;
            if (sourceCell != null) {
                if (targetCell == null) {
                    targetCell = targetRow.createCell(i);
                }
                //拷贝单元格，包括内容和样式
                copyCell(targetCell, sourceCell, targetWork, sourceWork);
            }
        }
    }

    /**
     * @return void
     * @Author csdn 博主:Keson Z
     * @Description 拷贝cell（单元格）
     * @Date 17:18 2021/5/11
     * @Param [targetCell, sourceCell, targetWork, sourceWork]
     **/
    public static void copyCell(Cell targetCell, Cell sourceCell, Workbook targetWork, Workbook sourceWork) {
        if (targetCell == null || sourceCell == null || targetWork == null || sourceWork == null) {
            throw new IllegalArgumentException("调用PoiUtil.copyCell()方法时，targetCell、sourceCell、targetWork、sourceWork都不能为空，故抛出该异常！");
        }

        CellStyle targetCellStyle = targetWork.createCellStyle();
        // 拷贝样式
        targetCellStyle.cloneStyleFrom(sourceCell.getCellStyle());
        targetCell.setCellStyle(targetCellStyle);
        try {
            targetCell.setCellValue(sourceCell.getStringCellValue());
        } catch (Exception e) {
            targetCell.setCellValue(sourceCell.getNumericCellValue());
        }
    }


}
